//

#ifndef WEBRTC_VAD_SIGNAL_PROCESS_H_
#define WEBRTC_VAD_SIGNAL_PROCESS_H_
#include <stdint.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C"{
#endif

#define RTC_COMPILE_ASSERT(expression) \
  switch (0) {                         \
    case 0:                            \
    case expression:;                  \
  }

#define WEBRTC_SPL_MUL(a, b) ((int32_t)((int32_t)(a) * (int32_t)(b)))

static __inline int WebRtcSpl_CountLeadingZeros32(uint32_t n){
	RTC_COMPILE_ASSERT(sizeof(unsigned int) == sizeof(uint32_t));
	return n == 0 ? 32 : __builtin_clz(n);
}

static __inline int16_t WebRtcSpl_GetSizeInBits(uint32_t n){
	return 32 - WebRtcSpl_CountLeadingZeros32(n);
}

static __inline int16_t WebRtcSpl_NormW32(int32_t a){
	return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a < 0 ? ~a : a) - 1;
}
static __inline int16_t WebRtcSpl_NormU32(uint32_t a) {
  return a == 0 ? 0 : WebRtcSpl_CountLeadingZeros32(a);
}

typedef struct {
  int32_t S_48_24[8];
  int32_t S_24_24[16];
  int32_t S_24_16[8];
  int32_t S_16_8[8];
} WebRtcSpl_State48khzTo8khz;

// allpass filter coefficients.
static const int16_t kResampleAllpass[2][3] = {
        {821, 6110, 12382},
        {3050, 9368, 15063}
};

// interpolation coefficients
static const int16_t kCoefficients48To32[2][8] = {
        {778, -2050, 1087, 23285, 12903, -3783, 441, 222},
        {222, 441, -3783, 12903, 23285, 1087, -2050, 778}
};

#define WEBRTC_SPL_WORD16_MAX 32767

int32_t WebRtcSpl_DivW32W16(int32_t num, int16_t den);
void WebRtcSpl_ResetResample48khzTo8khz(WebRtcSpl_State48khzTo8khz* state);
void WebRtcSpl_Resample48khzTo8khz(const int16_t* in, int16_t* out,
                                   WebRtcSpl_State48khzTo8khz* state, int32_t* tmpmem);
void WebRtcSpl_DownBy2IntToShort(int32_t *in, int32_t len, int16_t *out, int32_t *state);
void WebRtcSpl_LPBy2IntToInt(const int32_t* in, int32_t len, int32_t* out, int32_t* state);
int32_t WebRtcSpl_Energy(int16_t* vector, size_t vector_length, int* scale_factor);

#ifdef __cplusplus
}
#endif

#endif /* WEBRTC_VAD_SIGNAL_PROCESS_H_ */
